package cn.sdhuijin.vms.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.sdhuijin.vms.controller.comand.PageInfo;
import cn.sdhuijin.vms.model.ApproveStatus;
import cn.sdhuijin.vms.model.Leave;
import cn.sdhuijin.vms.model.User;
import cn.sdhuijin.vms.service.ILeaveService;

@Controller
@RequestMapping("/leave")
public class LeaveController {
	@Inject
	private ILeaveService leaveService;

	@RequestMapping(value = "", method = RequestMethod.GET)
	public String show() {
		return "leave/show";
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(PageInfo pageInfo, Model model) {
		Page<Leave> page = leaveService.getList(pageInfo.getPage(),
				pageInfo.getRows());
		model.addAttribute("page", page);
		return "leave/list";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(@ModelAttribute Leave leave) {
		return "leave/add";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public @ResponseBody JsonResult add(@Validated Leave leave,
			BindingResult error) {
		JsonResult jr = new JsonResult();
		if (error.hasErrors()) {
			StringBuffer sb = new StringBuffer();
			jr.setSuccess(false);
			List<FieldError> fes = error.getFieldErrors();
			for (FieldError fe : fes) {
				sb.append(fe.getDefaultMessage()).append(" ");
			}
			jr.setMessage(sb.toString());
		} else {
			User user = (User) SecurityUtils.getSubject().getPrincipal();
			leave.setApprovePerson(user);
			leave.setApproveTime(new Date());
			leaveService.save(leave);
			jr.setSuccess(true);
		}
		return jr;
	}

	@RequestMapping(value = "/approve/{leaveId}", method = RequestMethod.POST)
	public @ResponseBody JsonResult approve(@PathVariable Long leaveId,
			Boolean agree) {
		JsonResult jr = new JsonResult();
		Leave leave = leaveService.load(leaveId);

		if (leave.getStatus() == ApproveStatus.UNTREATED) {
			User user = (User) SecurityUtils.getSubject().getPrincipal();
			leave.setApprovePerson(user);
			leave.setApproveTime(new Date());
			leave.setStatus(agree ? ApproveStatus.AGREE
					: ApproveStatus.DISAGREE);
			leaveService.save(leave);
		} else {
			jr.setMessage("申请已被处理。");
			jr.setSuccess(false);
		}
		return jr;
	}

	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		CustomDateEditor dateEditor = new CustomDateEditor(fmt, true);
		fmt.setLenient(false);
		binder.registerCustomEditor(Date.class, dateEditor);
	}
}
